package com.lhkj.ct.base.model.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * @Description: 分页参数
 * @Author:
 * @Date: 2020/6/5 17:30
 */

public class PageQuery implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 第几页
     */
    @TableField(exist = false)
    private Integer page = 1;

    /**
     * 一页几行
     */
    @TableField(exist = false)
    private Integer limit = 25;

    /**
     * 关键词类型
     */
    @TableField(exist = false)
    private Integer keywordsType;

    /**
     * 关键词
     */
    @TableField(exist = false)
    private String keywords;

    /**
     * 日期类型
     */
    @TableField(exist = false)
    private Integer searchDateType;

    /**
     * 日期范围
     */
//    @ApiModelProperty(value = "日期范围", example = "2023-01-01 - 2023-02-01")
//    @TableField(exist = false)
//    private String searchDates;
    @ApiModelProperty(value = "开始时间", example = "2022-12-31")
    @TableField(exist = false)
    private Date beginTime;

    @ApiModelProperty(value = "结束时间", example = "2023-12-31")
    @TableField(exist = false)
    private Date endTime;

    /**
     * 日期分割
     */
//    @ApiModelProperty(value = "日期范围分割符", example = " - ")
//    @TableField(exist = false)
//    private String separator = " - ";

    /** 请求参数 */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @TableField(exist = false)
    private Map<String, Object> params;

    @ApiModelProperty(value = "排序列", example = "createTime")
    @TableField(exist = false)
    private String orderByColumn;

    @ApiModelProperty(value = "升/降序", allowableValues = "ascending,descending", example = "ascending")
    @TableField(exist = false)
    private String isAsc;

    public boolean isAsc() {
        if (StringUtils.isNotEmpty(isAsc)) {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc)) {
                return true;
            } else if ("descending".equals(isAsc)) {
                return false;
            }
        }
        return false;
    }

//    /**
//     * 排序
//     */
//    private OrderItem orders;

    public PageQuery(Integer page, Integer limit) {
        this.page = page;
        this.limit = limit;
        calculatePageAndLimit();
    }

    public PageQuery() {
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getKeywordsType() {
        return keywordsType;
    }

    public void setKeywordsType(Integer keywordsType) {
        this.keywordsType = keywordsType;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public Integer getSearchDateType() {
        return searchDateType;
    }

    public void setSearchDateType(Integer searchDateType) {
        this.searchDateType = searchDateType;
    }

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    //    public String getSearchDates() {
//        return searchDates;
//    }
//
//    public void setSearchDates(String searchDates) {
//        this.searchDates = searchDates;
//    }
//
//    public String getSeparator() {
//        return separator;
//    }
//
//    public void setSeparator(String separator) {
//        this.separator = separator;
//    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public String getOrderByColumn() {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn) {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc() {
        return isAsc;
    }

    public void setIsAsc(String isAsc) {
        this.isAsc = isAsc;
    }

    /**
     * 计算起止行号
     */
    public void calculatePageAndLimit() {
        this.page =  Optional.ofNullable(this.page).orElse(1);
        this.limit = Optional.ofNullable(this.limit).orElse(20);
    }

//    public <T> T paramsToBean(Class<T> clazz) {
//        return BeanUtil.mapToBean(this.params, clazz, false,CopyOptions.create());
//    }

//    public OrderItem getOrders() {
//        return orders;
//    }
//
//    public void setOrders(OrderItem orders) {
//        this.orders = orders;
//    }

//    @SafeVarargs
//    public final <T,R> PageQuery acs(SFunction<T, R>... orders) {
//        for (SFunction<T, R> order : orders) {
//            addOrder(OrderItem.asc(LambdaUtils.getUnderLineName(order)));
//        }
//        return this;
//    }
//
//    @SafeVarargs
//    public final <T,R> PageQuery desc(SFunction<T, R>... orders) {
//        for (SFunction<T, R> order : orders) {
//            addOrder(OrderItem.desc(LambdaUtils.getUnderLineName(order)));
//        }
//        return this;
//    }

//    public PageQuery addOrder(OrderItem orders) {
//        if (null == this.orders) {
//            this.orders = new ArrayList<>();
//        }
//        this.orders.add(orders);
//        return this;
//    }

//    public List<Date> convertDates() {
//        return searchDateList("yyyy-MM-dd");
//    }

//    public List<Date> convertDateTimes() {
//        List<Date> dates = searchDateList("yyyy-MM-dd");
//        if (!dates.isEmpty()) {
//            Date endDate = dates.get(1);
//            Calendar cal = Calendar.getInstance();
//            cal.setTime(endDate);
//            cal.set(Calendar.HOUR_OF_DAY, 23); //时
//            cal.set(Calendar.MINUTE, 59); //分
//            cal.set(Calendar.SECOND, 59); //秒
//            cal.set(Calendar.MILLISECOND, 999); //毫秒
//            Date endDateTime = cal.getTime();
//            dates.set(1, endDateTime);
//        }
//        return dates;
//    }

//    private List<Date> searchDateList(String... dateFormats) {
//        if (StringUtils.isNotBlank(searchDates)){
//            String[] split = searchDates.split(separator);
//            if (split.length == 2) {
//                try {
//                    Date begin = DateUtils.parseDate(split[0], dateFormats);
//                    Date end = DateUtils.parseDate(split[1], dateFormats);
//                    return Lists.newArrayList(begin, end);
//                } catch (ParseException e) {
//                    return new ArrayList<>();
//                }
//            }
//        }
//        return new ArrayList<>();
//    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

